package com.smy.baseutil.entity;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSON;

public class Student {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6856501273610085027L;

	private String name;
	
	private String age;
	
	private String sex;
	
	@Secret
	private String phone;

	
	public Student(String name, String age, String sex, String phone) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.phone = phone;
	}

	public Student() {
		// TODO Auto-generated constructor stub
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	private String toString0() {
		Method m;
		boolean isEnd = true;
		Object[] params = new Object[0];
		//StringBuilder sb = new StringBuilder(simpleName + "{");
		StringBuilder sb = new StringBuilder("{");
		try {
			PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(getClass());
			for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
				if (!"class".equals(propertyDescriptor.getName())) {
					m = propertyDescriptor.getReadMethod();
					if (!m.isAccessible()) {
						m.setAccessible(true);
					}
					Object result = m.invoke(this, params);
					if (!isEnd) {
						sb.append(",");
					} else {
						isEnd = false;
					}
					Field feild = this.getClass().getDeclaredField(propertyDescriptor.getName());
					Secret annotation = feild.getAnnotation(Secret.class);
					if (annotation == null) {
						sb.append("\"" + propertyDescriptor.getName() + "\"").append(":\"" + result + "\"");
					} else {
						sb.append("\"" + propertyDescriptor.getName() + "\"").append(":\"******\"");
					}

					m.setAccessible(false);
				}
			}
			sb.append("}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public static void main(String[] args)  {
		Student s = new Student();
		System.err.println(s.toString0());
		System.err.println(JSON.toJSONString(s));
	}
}
